package com.yc.javaee.servlet.d0617;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 名为 [LoginServlet]和 [LoginServlet1] 的
 *      servlet不能映射为一个url模式(url-pattern) [/login.s]
 *  强调: Servlet的地址全局唯一, 不能重复
 */
@WebServlet(name = "LoginServlet1", value = "/d0617/login.s")
public class LoginServlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 表单方式请求
         */
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String vcode = request.getParameter("vcode");
        HttpSession session = request.getSession();
        // 从会话中获取验证码
        String sessionVcode = (String) session.getAttribute("vcode");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if(sessionVcode.equalsIgnoreCase(vcode) == false){
            out.append("<div style='margin:100px;text-align:center;color:red'>")
                    .append("验证码错误!").append("<div>");
        } else if(!"yc".equals(name) || !"123".equals(pwd)){
            out.append("<div style='margin:100px;text-align:center;color:red'>")
                    .append("用户名或密码错误!").append("<div>");
        } else {
            out.append("<div style='margin:100px;text-align:center'>")
                    .append("登录成功!").append("<div>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
